/*
 * @Author: zpz
 * @Date: 2019-08-15 15:46:43
 * @LastEditors: Bingo
 * @Email: 13714943895@163.com
 */
// import { login, getInfo, deptInfo, deptList, currentDept, autoLogin } from '@api/login';
// import { apiRoleGroupInfo } from '@api/gcc';
import { ACCESS_TOKEN, RESET_PWD, USER_MENU } from "@/config/const";
// import { welcome } from '@/utils/util';
import {useCookies} from 'vue3-cookies';
const {cookies} = useCookies()
const user = {
    namespaced: true,
	state:()=>({
		token: '',
		name: '',
		login: '',
		deptInfo: {},
		deptList: [],
		welcome: '',
		avatar: '',
		roles: [],
		info: {},
		userMenu: [], // 用户菜单（未遍历）,
		pageAuths: {}, // 当前用户所在页面拥有的权限
		isShowLogin: false,
		userType: ''
	}),
	mutations: {
		SHOWLOGIN: (state: { isShowLogin: boolean; }) => {
			state.isShowLogin = true;
		},
		HIDELOGIN: (state: { isShowLogin: boolean; }) => {
			state.isShowLogin = false;
		},
		SET_TOKEN: (state: { token: any; }, token: any) => {
			state.token = token;
		},
		SET_NAME: (state: { name: any; login: any; welcome: any; userType: any; }, { name, login, welcome, userType }: any) => {
			state.name = name;
			state.login = login;
			state.welcome = welcome;
			state.userType = userType;
		},
		SET_AVATAR: (state: { avatar: any; }, avatar: any) => {
			state.avatar = avatar;
		},
		SET_ROLES: (state: { roles: any; }, roles: any) => {
			state.roles = roles;
			localStorage.setItem('roles', JSON.stringify(roles));
		},
		SET_INFO: (state: { info: any; }, info: any) => {
			state.info = info;
		},
		SET_MENU: (state: { userMenu: any; }, userMenu: any) => {
			state.userMenu = userMenu;
		},
		SET_PAGE_AUTH: (state: { pageAuths: any; }, obj: any) => {
			state.pageAuths = obj;
		},
		SET_DEPT_INFO: (state: { deptInfo: any; }, obj: any) => {
			state.deptInfo = obj;
		},
		SET_DEPT_LIST: (state: { deptList: any; }, obj: any) => {
			state.deptList = obj;
		}
	},

	actions: {
		// 登录
		Login({ commit }: any, userInfo: any) {
			return new Promise<void>((resolve, reject) => {
				// login(userInfo)
				// 	.then((response: { menus: any; jwt: any; resetPwd: any; roles: any; }) => {
				// 		const userMenu = response.menus;
				// 		const token = response.jwt;
				// 		const resetPwd = response.resetPwd;
				// 		// 存储tk
				// 		localStorage.set(ACCESS_TOKEN, token);
				// 		// 存储用户的动态路由表 这里用ls而不是store是因为 store刷新后里面的数据会丢失
				// 		localStorage.set(USER_MENU, userMenu);
				// 		localStorage.set(RESET_PWD, resetPwd);
				// 		sessionStorage.setItem('LtpaToken', cookies.get('LtpaToken'));
				// 		commit('SET_TOKEN', token);
				// 		commit('SET_ROLES', response.roles);
				// 		// 获取所在部门信息
				// 		deptInfo().then(
				// 			(res: any) => {
				// 				localStorage.set('DEPT_INFO', res);
				// 			},
				// 			(err: any) => { }
				// 		);
				// 		resolve();
				// 	})
				// 	.catch((error: any) => {
				// 		cookies.set('LtpaToken', null, '1s', '/', '.konka.com');
				// 		reject(error);
				// 	});
			});
		},

		// 登录
		AutoLogin({ dispatch, commit }:any) {
			console.log('我被调用了--------------------')
			return new Promise<void>((resolve, reject) => {
				// autoLogin()
				// 	.then((response: { menus: any; jwt: any; resetPwd: any; }) => {
				// 		const userMenu = response.menus;
				// 		const token = response.jwt;
				// 		const resetPwd = response.resetPwd;
				// 		localStorage.set(ACCESS_TOKEN, token);
				// 		localStorage.set(USER_MENU, userMenu);
				// 		localStorage.set(RESET_PWD, resetPwd);
				// 		sessionStorage.setItem('LtpaToken', cookies.get('LtpaToken'));

				// 		console.log('我被调用了autoLogin--------------------')
				// 		commit('SET_TOKEN', token);
				// 		// 获取所在部门信息
				// 		deptInfo().then(
				// 			(res: any) => {
				// 				localStorage.set('DEPT_INFO', res);
				// 			},
				// 			(err: any) => { }
				// 		);
				// 		resolve();
				// 	}).catch((error: any) => {
				// 		// cookies.set('LtpaToken', null, '1s', '/', '.konka.com');
				// 		dispatch('Logout').then(() => {
				// 			// 退出登录后刷新页面
				// 			location.reload();
				// 			// console.log('退出登录')
				// 		});
				// 		reject(error);
				// 	});
			});
		},

		// 获取用户信息
		GetInfo({ commit }:any) {
			return new Promise((resolve, reject) => {
				// getInfo()
				// 	.then((response: unknown) => {
				// 		const result:any = response;
				// 		commit('SET_INFO', result);
				// 		apiRoleGroupInfo(result.login).then((res: any) => {
				// 			localStorage.set('SET_ORG_INFO', res);
				// 		});
				// 		localStorage.set('SET_INFO', result);
				// 		commit('SET_NAME', {
				// 			name: result.username,
				// 			login: result.login,
				// 			welcome: welcome(),
				// 			userType: result.userType
				// 		});
				// 		commit('SET_AVATAR', result.avatar || './logo.png');
				// 		commit('SET_DEPT_INFO', result.redisOrganizationDTO);
				// 		localStorage.set('DEPT_INFO', result.redisOrganizationDTO);
				// 		resolve(response);
				// 	})
				// 	.catch((error: any) => {
				// 		reject(error);
				// 	});
			});
		},

		// 获取用户所在部门信息
		GetDeptList({ commit }:any) {
			return new Promise((resolve, reject) => {
				// deptList()
				// 	.then((response: unknown) => {
				// 		const result = response;
				// 		commit('SET_DEPT_LIST', result);
				// 		localStorage.set('DEPT_LIST', result);
				// 		resolve(response);
				// 	})
				// 	.catch((error: any) => {
				// 		reject(error);
				// 	});
			});
		},

		// 切换部门
		changeDept({ commit }: any, orgId: any) {
			return new Promise((resolve, reject) => {
				// currentDept(orgId)
				// 	.then((response: unknown) => {
				// 		resolve(response);
				// 	})
				// 	.catch((error: any) => {
				// 		reject(error);
				// 	});
			});
		},

		// 登出
		Logout({ commit, state }:any, isClear = true) {
			return new Promise<void>((resolve) => {
				commit('SET_TOKEN', '');
				commit('SET_ROLES', []);
				commit('SET_MENU', []);
				localStorage.remove(ACCESS_TOKEN);
				localStorage.remove(USER_MENU);
				localStorage.remove(RESET_PWD);
				if (isClear) {
					cookies.set('LtpaToken', null, '1s', '/', '.konka.com');
				}
				// 清空之前先保存
				let loginAccount = JSON.parse(localStorage.getItem('pro__SET_INFO')||'{}').value.login
				let lcHomePageName = loginAccount + '__INTRO__homePage'
				let lcsSartPageName = loginAccount + '__INTRO__startPage'

				localStorage.clear();
				sessionStorage.clear();

				localStorage.setItem(lcHomePageName, '1')
				localStorage.setItem(lcsSartPageName, '1')

				resolve();
			});
		}
	}
};

export default user;
